/**
 * 深度克隆方法 会在vue上进行挂载，使用方法this.$deepCopy(obj)
 **/
const deepCopy = (obj, cache = []) => {
	if (obj === null || typeof obj !== 'object') {
		return obj;
	}

	const objType = Object.prototype.toString.call(obj).slice(8, -1);

	// 考虑 正则对象的copy
	if (objType === 'RegExp') {
		return new RegExp(obj);
	}

	// 考虑 Date 实例 copy
	if (objType === 'Date') {
		return new Date(obj);
	}

	// 考虑 Error 实例 copy
	if (objType === 'Error') {
		return new Error(obj);
	}

	const hit = cache.filter((c) => c.original === obj)[0];

	if (hit) {
		return hit.copy;
	}

	const copy = Array.isArray(obj) ? [] : {};

	cache.push({ original: obj, copy });

	Object.keys(obj).forEach((key) => {
		copy[key] = deepCopy(obj[key], cache);
	});

	return copy;
}
/**
 * 防抖函数 this.$debounce(fn)
 **/
const debounce = (fn, delay = 1000) => {
	let timer
	return function () {
		if (timer) {
			clearTimeout(timer)
		}
		timer = setTimeout(() => {
			fn()
		}, delay)
	}
}
/**
 * 获取应用id
 **/
const getApplicationId = (name) => {
	const applicationNameList = applicationList.applicationList;
	const list = JSON.parse(localStorage.getItem('categoryIdList'));
	let id = '';
	if (list && list.length > 0) list.forEach(item => {
		if (applicationNameList[name]) {
			if (item.name == applicationNameList[name].appName && item.list.length > 0) item.list.forEach(child => {
				if (child.appName == applicationNameList[name].appName && child.categoryName == applicationNameList[name].categoryName) id = child.id;
			});
		}
	})
	return id;
}


/**导出**/
export default {
	deepCopy,
	debounce,
}

// 存取token
const TokenKey = 'x-access-token'
export function getToken() {
	// return Cookies.get(TokenKey)
	return localStorage.getItem(TokenKey);
}

export function setToken(token) {
	var inFifteenMinutes = new Date(new Date().getTime() + 120 * 60 * 1000);
	// return Cookies.set(TokenKey, token, { expires: inFifteenMinutes })
	return localStorage.setItem(TokenKey, token)
}

export function removeToken() {
	// return Cookies.remove(TokenKey)
	return localStorage.removeItem(TokenKey);
}

